package com.ljw.vendingmachine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ljw.vendingmachine.base.BaseActivity;
import com.ljw.vendingmachine.service.WebSocketService;
import com.ljw.vendingmachine.test.GeziTestActivity;
import com.ljw.vendingmachine.utils.Constant;
import com.ljw.vendingmachine.utils.SPUtils;

import butterknife.BindView;

public class SelectSetting extends BaseActivity {
    @BindView(R.id.layout_password)
    LinearLayout layout_password;
    @BindView(R.id.ed_password)
    EditText ed_password;
    @BindView(R.id.btn_yes)
    Button btn_yes;
    @BindView(R.id.layout_settings)
    LinearLayout layout_settings;
    @BindView(R.id.btn_ipAndPort)
    Button btn_ipAndPort;
    @BindView(R.id.btn_other)
    Button btn_other;

    @BindView(R.id.btn_gezi_test)
    Button btn_gezi_test;
    @Override
    public int setContentView() {
        return R.layout.activity_select_setting;
    }

    String TAG = "SelectSetting";
    @Override
    public void initView() {

        if (SPUtils.contains(this, Constant.PASSWORD)){
            layout_password.setVisibility(View.VISIBLE);
            layout_settings.setVisibility(View.GONE);
        }else {
            layout_password.setVisibility(View.GONE);
            layout_settings.setVisibility(View.VISIBLE);
        }

        btn_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = ed_password.getText().toString().trim();
                Log.i(TAG,SPUtils.get(SelectSetting.this,Constant.PASSWORD,"").toString());
//                password = new Sha256Hash(password, userEntity.getSalt()).toHex();
                if (!TextUtils.isEmpty(password)&& !SPUtils.get(SelectSetting.this,Constant.PASSWORD,"").toString().equals(password)){
                    layout_password.setVisibility(View.GONE);
                    layout_settings.setVisibility(View.VISIBLE);
                }else{
                    Toast.makeText(SelectSetting.this,"密码错误",Toast.LENGTH_LONG).show();
                }
            }
        });
        btn_ipAndPort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SelectSetting.this,ConnectSettingActivity.class));
                finish();
            }
        });
        btn_other.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!SPUtils.contains(SelectSetting.this,Constant.IP)) {
                    Toast.makeText(SelectSetting.this,"必须设置连接的IP",Toast.LENGTH_LONG).show();
                    return;
                }
                startActivity(new Intent(SelectSetting.this,OtherSettingActivity.class));
                finish();
            }
        });
        btn_gezi_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!SPUtils.contains(SelectSetting.this,Constant.IP)) {
                    Toast.makeText(SelectSetting.this,"必须设置连接的IP",Toast.LENGTH_LONG).show();
                   return;
                }
                startActivity(new Intent(SelectSetting.this,GeziTestActivity.class));
            }
        });
    }

    @Override
    public void initData() {
        if (!SPUtils.contains(SelectSetting.this,Constant.IP)) {
            Toast.makeText(SelectSetting.this,"必须设置连接的IP",Toast.LENGTH_LONG).show();

        }else {
            StartWebSocketService();
        }

    }
    /*开启websocket 服务
     */
    public void StartWebSocketService() {
        Intent intent = new Intent(this, WebSocketService.class);
        startService(intent);
    }
}
